# import tkinter as tk
# from tkinter import messagebox

# class Calculator:
#     def __init__(self, root):
#         self.root = root
#         self.root.title("计算器")
#         self.root.geometry("360x500")


#         # 创建菜单
#         menu_bar = tk.Menu(self.root)
#         self.root.config(menu=menu_bar)
        
#         menu = tk.Menu(menu_bar, tearoff=0)
#         menu_bar.add_cascade(label="菜单", menu=menu)
#         menu.add_command(label="退出", command=self.quit_app)

#         # 创建文本框
#         self.expression = tk.StringVar()
#         self.entry = tk.Entry(self.root, textvariable=self.expression, font=('Arial', 24), bd=10, insertwidth=4, width=14, borderwidth=4)
#         self.entry.grid(row=0, column=0, columnspan=4)

#         # 创建按钮框架
#         frame1 = tk.Frame(self.root)
#         frame1.grid(row=1, column=0, columnspan=4, pady=10)

#         frame2 = tk.Frame(self.root)
#         frame2.grid(row=2, column=0, columnspan=4, pady=10)

#         # 创建回退和清空按钮
#         self.back_button = tk.Button(frame1, text="回退", command=self.back_space, font=('Arial', 18), width=10, height=2)
#         self.back_button.grid(row=0, column=0)

#         self.clear_button = tk.Button(frame1, text="清空", command=self.clear_expression, font=('Arial', 18), width=10, height=2)
#         self.clear_button.grid(row=0, column=1)

#         # 创建数字和运算符按钮
#         buttons = [
#             '7', '8', '9', '/', 
#             '4', '5', '6', '*', 
#             '1', '2', '3', '-', 
#             '0', '.', '=', '+'
#         ]

#         row_val, col_val = 0, 0
#         for button in buttons:
#             action = lambda x=button: self.append_character(x)
#             tk.Button(frame2, text=button, command=action, font=('Arial', 18), width=10, height=2).grid(row=row_val, column=col_val)
#             col_val += 1
#             if col_val > 3:
#                 col_val = 0
#                 row_val += 1

#     def append_character(self, char):
#         current_text = self.expression.get()
#         if char == "=":
#             try:
#                 result = eval(current_text)
#                 self.expression.set(str(result))
#             except Exception as e:
#                 messagebox.showerror("错误", "无效的表达式")
#         else:
#             self.expression.set(current_text + char)

#     def back_space(self):
#         current_text = self.expression.get()
#         if current_text:
#             self.expression.set(current_text[:-1])

#     def clear_expression(self):
#         self.expression.set("")

#     def quit_app(self):
#         self.root.quit()

# if __name__ == "__main__":
#     root = tk.Tk()
#     calc = Calculator(root)
#     root.mainloop()








import tkinter as tk
from tkinter import filedialog, messagebox, Menu
from tkinter.scrolledtext import ScrolledText


class SimpleNotepad:
    def __init__(self, root):
        self.root = root
        self.root.title("简易记事本")
        self.root.geometry("600x400")

        self.text_area = ScrolledText(self.root, wrap=tk.WORD, undo=True)
        self.text_area.pack(expand=1, fill='both')

        self.menu_bar = Menu(self.root)
        self.root.config(menu=self.menu_bar)

        self.file_menu = Menu(self.menu_bar, tearoff=0)
        self.menu_bar.add_cascade(label="文件", menu=self.file_menu)
        self.file_menu.add_command(label="新建", command=self.new_file)
        self.file_menu.add_command(label="打开", command=self.open_file)
        self.file_menu.add_command(label="保存", command=self.save_file)
        self.file_menu.add_separator()
        self.file_menu.add_command(label="退出", command=self.exit_app)

        self.edit_menu = Menu(self.menu_bar, tearoff=0)
        self.menu_bar.add_cascade(label="编辑", menu=self.edit_menu)
        self.edit_menu.add_command(label="撤销", command=self.undo)
        self.edit_menu.add_command(label="重做", command=self.redo)
        self.edit_menu.add_separator()
        self.edit_menu.add_command(label="剪切", command=self.cut)
        self.edit_menu.add_command(label="复制", command=self.copy)
        self.edit_menu.add_command(label="粘贴", command=self.paste)

        self.file_path = None

    def new_file(self):
        self.text_area.delete(1.0, tk.END)
        self.file_path = None

    def open_file(self):
        file_path = filedialog.askopenfilename(defaultextension=".txt",
                                               filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
        if file_path:
            with open(file_path, 'r', encoding='utf-8') as file:
                content = file.read()
                self.text_area.delete(1.0, tk.END)
                self.text_area.insert(tk.END, content)
            self.file_path = file_path

    def save_file(self):
        if self.file_path:
            with open(self.file_path, 'w', encoding='utf-8') as file:
                content = self.text_area.get(1.0, tk.END)
                file.write(content)
        else:
            self.save_as_file()

    def save_as_file(self):
        file_path = filedialog.asksaveasfilename(defaultextension=".txt",
                                                 filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
        if file_path:
            with open(file_path, 'w', encoding='utf-8') as file:
                content = self.text_area.get(1.0, tk.END)
                file.write(content)
            self.file_path = file_path

    def exit_app(self):
        if messagebox.askokcancel("退出", "你确定要退出吗？"):
            self.root.quit()

    def undo(self):
        try:
            self.text_area.edit_undo()
        except tk.TclError:
            messagebox.showwarning("撤销", "没有可以撤销的操作")

    def redo(self):
        try:
            self.text_area.edit_redo()
        except tk.TclError:
            messagebox.showwarning("重做", "没有可以重做的操作")

    def cut(self):
        try:
            self.text_area.event_generate("<<Cut>>")
        except tk.TclError:
            messagebox.showwarning("剪切", "没有选中的文本可以剪切")

    def copy(self):
        try:
            self.text_area.event_generate("<<Copy>>")
        except tk.TclError:
            messagebox.showwarning("复制", "没有选中的文本可以复制")

    def paste(self):
        try:
            self.text_area.event_generate("<<Paste>>")
        except tk.TclError:
            messagebox.showwarning("粘贴", "剪贴板中没有可以粘贴的内容")

if __name__ == "__main__":
    root = tk.Tk()
    app = SimpleNotepad(root)
    root.mainloop()